<template>
  <div id="Login">
    <mt-header title="登录">
      <router-link to="/" slot="left">
        <mt-button icon="back">返回</mt-button>
      </router-link>
      <!-- <mt-button icon="more" slot="right"></mt-button> -->
    </mt-header>
    <h2>登录页</h2>
    <hr />
    <!-- <img src="../assets/3.jpg" alt="">
        
        <mt-field label="手机号" placeholder="请输入手机号" type="tel" v-model="phone"></mt-field>
        <mt-field label="密码" placeholder="请输入密码" type="password" v-model="password"></mt-field>
        <hr>
        <a>短信验证码登录</a><a @click="register()" class="reg">免费注册</a><br>
        <button class="btn" @click="login()">登录</button> -->

    <van-form @submit="onSubmit">
      <van-field
        v-model="phone"
        name="手机号"
        label="手机号"
        placeholder="请输入手机号"
        :rules="[
          { required: true, message: '请填写手机号' },
          { pattern: /^1[356789]\d{9}$/, message: '手机号格式错误' },
        ]"
      />
      <van-field
        v-model="password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit">提交</van-button>
        <a @click="code()">短信验证码登录</a
        ><a @click="register()" class="reg">免费注册</a><br />
      </div>
    </van-form>
  </div>
</template>

<script>
import url from "../tools/url.js";
import { Toast } from "mint-ui";
export default {
  name: "Login",
  data() {
    return {
      phone: "",
      password: "",
    };
  },
  methods: {
    register() {
      this.$router.push({
        path: "/Register",
      });
    },
    // 验证码登录
    code() {
      this.$router.push("/YanZheng");
    },
    onSubmit() {
      //发请求
      var str = `phone=${this.phone}&password=${this.password}`;
      this.axios.post(url.LOGINBYPASSWORD, str).then((res) => {
        if (res.data.code == 200) {
          Toast(res.data.data.message);
          //把token存到浏览器
          sessionStorage.setItem("token", res.data.data.token);
          //把用户信息存入vuex
          this.$store.commit("save", res.data.data);
          //跳转至首页
          this.$router.push("/");
        } else {
          Toast(res.data.data.message);
        }
      });
    },
  },
};
</script>
<style scoped>
h2 {
  margin-top: 40px;
}
img {
  border-radius: 50%;
  overflow: hidden;
  width: 90px;
  height: 83px;
  margin-left: 170px;
}
.reg {
  float: right;
  margin-right: 10px;
}
.btn {
  background: -webkit-linear-gradient(left, #ff9000, #ff5000) no-repeat;
  color: #fff;
  box-shadow: 0 2px 4px #f7c7b1;
  height: 45px;
  border-radius: 35px;
  margin-top: 15px;
  font-size: 16px;
  width: 100%;
  cursor: pointer;
}
</style>
